using System;
using System.ComponentModel;
using System.Windows.Forms;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.UI
{
    /// <summary>
    /// The form hosting the wizard
    /// </summary>
    public partial class WizardForm : Form
    {
        private readonly GenerationArgs _args = new GenerationArgs();

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public WizardForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="args"></param>
        public WizardForm(GenerationArgs args)
        {
            InitializeComponent();
            if (!DesignMode)
            {
                _args = args;
            }
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public void GenerationComplete()
        {
            BuildFileGenerationWizard.SelectedPage = BuildFileGenerationWizard.SelectedPage.NextPage;
        }

        /// <summary>
        /// Cancels the wizard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Wizard1Cancel(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Wizard load form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WizardFormLoad(object sender, EventArgs e)
        {
            if (DesignMode) return;
            BuildFileGenerationWizard.SelectedPage = IntroductionPage;
            RegisterArgsWithControls();
            SetupWizardSteps();
            generateFilesControl1.GenerationComplete +=
                GenerationComplete;
        }

        #region Private Methods

        /// <summary>        
        /// </summary>
        private void SetupWizardSteps()
        {
            //Next steps
            IntroductionPage.NextPage = ChooseApplicationPage;
            ChooseApplicationPage.NextPage = BuildPropertiesWizardPage;
            BuildPropertiesWizardPage.NextPage = BuildReferencesPage;
            BuildReferencesPage.NextPage = WebServicePublishingPage;
            WebServicePublishingPage.NextPage = bizTalkHostsPage;
            bizTalkHostsPage.NextPage = ApplicationBindingsPage;
            ApplicationBindingsPage.NextPage = ApplicationDeploymentPage;
            ApplicationDeploymentPage.NextPage = UnitTestingPage;
            UnitTestingPage.NextPage = GenerationOptionsWizardPage;
            GenerationOptionsWizardPage.NextPage = GenerateFilesPage;
            GenerateFilesPage.NextPage = FinalPage;

            //Previous steps
            IntroductionPage.PreviousPage = null;
            ChooseApplicationPage.PreviousPage = IntroductionPage;
            BuildPropertiesWizardPage.PreviousPage = ChooseApplicationPage;
            BuildReferencesPage.PreviousPage = BuildPropertiesWizardPage;
            WebServicePublishingPage.PreviousPage = BuildReferencesPage;
            bizTalkHostsPage.PreviousPage = WebServicePublishingPage;
            ApplicationBindingsPage.PreviousPage = bizTalkHostsPage;
            ApplicationDeploymentPage.PreviousPage = ApplicationBindingsPage;
            UnitTestingPage.PreviousPage = ApplicationDeploymentPage;
            GenerationOptionsWizardPage.PreviousPage = UnitTestingPage;
            GenerateFilesPage.PreviousPage = GenerationOptionsWizardPage;
            FinalPage.PreviousPage = GenerateFilesPage;
        }
        /// <summary>
        /// Registers the appropriate data capture object with its control
        /// </summary>
        private void RegisterArgsWithControls()
        {
            referencesControl1.RegisterReferences(_args.BuildReferences);
            applicationBindingsControl1.RegisterBindings(_args.ApplicationBindings);
            applicationDeploymentControl1.RegisterDeployment(_args.ApplicationDeployment);
            applicationDescriptionControl1.RegisterArgs(_args);
            publishWebServicesControl1.RegisterWebServices(_args);
            unitTestingControl.RegisterUnitTesting(_args.UnitTesting);
            generateFilesControl1.RegisterArgs(_args);
            bizTalkHostsControl1.RegisterHosts(_args);
            generationOptionsControl1.RegisterArgs(_args);
            buildPropertiesControl1.RegisterBuildProperties(_args.BuildProperties);
        }

        #endregion

        #region Wizard Before Display Events

        private static void ApplicationDeploymentPageBeforeDisplay(object sender, EventArgs e)
        {
        }

        private static void UnitTestingPageBeforeDisplay(object sender, EventArgs e)
        {
        }

        private static void FinalPageBeforeDisplay(object sender, EventArgs e)
        {
        }

        #endregion

        #region Wizard Before Move Next Handlers

        private void ChooseApplicationPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = applicationDescriptionControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void BuildReferencesPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = referencesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void WebServicePublishingPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = publishWebServicesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void ApplicationBindingsPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = applicationBindingsControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void ApplicationDeploymentPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = applicationDeploymentControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void UnitTestingPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = unitTestingControl.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        /// <summary>
        /// Before generated files moves next
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateFilesPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = generateFilesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;

            GenerateFilesPage.AllowMoveNext = false;
        }

        private void FinalPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var dr =
                MessageBox.Show(@"Would you like to save the build so it can be used again?", @"Save",
                                MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                dr = saveArgs.ShowDialog();
                if (dr == DialogResult.OK)
                    ArgsHelper.Save(saveArgs.FileName, _args);
            }
            Close();
        }
        private void GenerationOptionsWizardPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = generationOptionsControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }
        private void BuildPropertiesWizardPageBeforeMoveNext(object sender, CancelEventArgs e)
        {
            var result = buildPropertiesControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }
        #endregion

        

        
    }
}